File Dialog Example

Description

This demonstrates the ability for the user to show the file dialog and return the selected file path value.

Python Script Files

  • file_dialog_ui.py

Significant Script Features

file_dialog_ui.py:

​ # Define Layout and Components

def getCustomToolPropertyContent ( ):

# Create a Grid

my_Grid = WPFControls.Grid ()

# Add 2 Rows and 1 Column

my_ Grid.RowDefinitions.Add ( WPFControls.RowDefinition ())

my_ Grid.RowDefinitions.Add ( WPFControls.RowDefinition ())

my_ Grid.ColumnDefinitions.Add ( WPFControls.ColumnDefinition ())

# Create an empty input TextBox to display the file name

# Assign it to Row 0, Column 0

global fileNameTextBox

fileNameTextBox = WPFControls.TextBox ()

WPFControls.Grid.SetRow ( fileNameTextBox , 0)

WPFControls.Grid.SetColumn ( fileNameTextBox , 0)

# Create a button and set it's text to "Import"

# Assign it to Row1, Column 0

importBtn = WPFControls.Button ()

importBtn.Content = "Import"

WPFControls.Grid.SetRow ( importBtn , 1)

WPFControls.Grid.SetColumn ( importBtn , 0)

# Link a function to the Button "Click" event

# This function will be called every time the Button is clicked

importBtn.Click += HandleImportButton

# Add the controls to the Grid

my_ Grid.Children.Add ( fileNameTextBox )

my_ Grid.Children.Add ( importBtn )

# Return the Grid

return my_Grid

# Function to handle the Import Button "Click" event

# This function gets called every time the Button is clicked

@apex_ sdk.errorhandler

def HandleImportButton ( sender,args ):

# Create a File open dialog

dialog = OpenFileDialog ( )

# Configure for single file selection

dialog.Multiselect = False

# Set up the file types you want to support

dialog.Filter = "CSV Files|*. csv|All Files|*.*"

# Display the dialog

# If it returns anything

# 1) Get the file name

# 2) Display it on the text box

if dialog.ShowDialog ():

selectedFile = dialog.FileName

fileNameTextBox.Text = selectedFile

Notes

  • The importBtn button is registered with a click listener HandleImportButton ​ function.
  • The HandleImportButton function displays the file dialog.